package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.net.URLEncoder;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.common.Util;
import com.glowdayz.glowmee.dao.PouchDAO;

public class InsertTodayPouchBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
    
    public InsertTodayPouchBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		String id_str = request.getParameter("id");
		String isToday_str = request.getParameter("isToday");
		
		if (id_str != null && isToday_str != null) {
			int id = Integer.parseInt(id_str);
			int isToday = Integer.parseInt(isToday_str);
			try {
				PouchDAO.getInstance().isTodayPouch(id, isToday);
				request.setAttribute("alert", "<script type='text/javascript'>self.window.alert('처리가 완료되었습니다.');"
						+ "opener.location.reload();window.close();</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String where = request.getParameter("where");
		if (where != null) {
			where = Util.getInstance().incoding(where);
		} else {
			where = "";
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			if (!where.equals("")) request.setAttribute("where", "&where=" + URLEncoder.encode(where, "UTF-8"));
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("todayPouch", PouchDAO.getInstance().getTodayPouchList(page, 10, where));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/pouch/insertTodayPouch.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		String where = "";
		
		String userType = request.getParameter("userType");
		String created_date1 = request.getParameter("created_date1");
		String created_date2 = request.getParameter("created_date2");
		String nickName = request.getParameter("nickName");
		String pouchCategory = request.getParameter("pouchCategory");
		pouchCategory = Util.getInstance().incoding(pouchCategory);
		if (userType != null && !userType.equals("")) {
			userType = Util.getInstance().incoding(userType);
			where += " AND r.userType = '" + userType + "'";
		}
		if (created_date1 != null && !created_date1.equals("") && created_date2 != null && !created_date2.equals("")) {
			created_date1 = created_date1.replace("-", "");
			created_date1 += "000000";
			created_date2 += "999999";
			created_date2 = created_date2.replace("-", "");
			where += " AND p.create_date BETWEEN " + created_date1 + " AND " + created_date2;
		}
		if (nickName != null && !nickName.equals("")) {
			nickName = Util.getInstance().incoding(nickName);
			where += " AND r.nickName like '%" + nickName + "%'";
		}
		if (pouchCategory != null && !pouchCategory.equals("--- 선택하세요 ---")) {
			where += " AND p.idPouchCategory = " + pouchCategory;
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			if (!where.equals("")) request.setAttribute("where", "&where=" + URLEncoder.encode(where, "UTF-8"));
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("todayPouch", PouchDAO.getInstance().getTodayPouchList(page, 10, where));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/pouch/insertTodayPouch.jsp");
		dpr.forward(request, response);
	}
	
}
